<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- 加部门名、来源部门、去向部门 -->
       <el-form-item label="部门名" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择部门"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in deptList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
       </el-form-item>
       <el-form-item label="来源部门" prop="fromDeptId">
        <el-select
          v-model="queryParams.fromDeptId"
          placeholder="请选择来源部门"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in deptList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
       </el-select>
      </el-form-item>
      <el-form-item label="去向部门" prop="toDeptId">
        <el-select
          v-model="queryParams.toDeptId"
          placeholder="请选择去向部门"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item in deptList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
       </el-form-item>
      <el-form-item label="事务类型" prop="affairs">
        <el-input
          v-model="queryParams.affairs"
          placeholder="请输入事务类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出入库单号" prop="affairsId" v-if="false">
        <el-input
          v-model="queryParams.affairsId"
          placeholder="请输入出入库单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productId" v-if="false">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品单位" prop="productUnit" v-if="false">
        <el-input
          v-model="queryParams.productUnit"
          placeholder="请输入产品单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productStandards" v-if="false">
        <el-input
          v-model="queryParams.productStandards"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="销售价格" prop="productPrice" v-if="false">
        <el-input
          v-model="queryParams.productPrice"
          placeholder="请输入销售价格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品批次" prop="productBatch">
        <el-input
          v-model="queryParams.productBatch"
          placeholder="请输入产品批次"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="productValidityDate">
        <el-date-picker
          v-model="queryParams.productValidityDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="数量" prop="count">
        <el-input
          v-model="queryParams.count"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark" v-if="false">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" v-if="false">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="流程实例的编号" prop="processInstanceId">
        <el-input
          v-model="queryParams.processInstanceId"
          placeholder="请输入流程实例的编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="创建者" prop="creator" v-if="false">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建者"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:sec-account:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:sec-account:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" type="index" width="60" />
      <el-table-column label="部门名" align="center" prop="deptName" width="100%" />
      <el-table-column label="来源部门" align="center" prop="fromDeptName" width="100%" />
      <el-table-column label="去向部门" align="center" prop="toDeptName" width="100%" />
      <el-table-column label="产品名称" align="center" prop="productName" width="100%" />
      <el-table-column label="事务类型" align="center" prop="affairs" width="100%" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="单位名称" align="center" prop="productUnitName" width="100%" />
      <!-- <el-table-column label="事务编号" align="center" prop="affairsId" /> -->
      <!-- <el-table-column label="产品编号" align="center" prop="productId" /> -->
      <!-- <el-table-column label="产品单位" align="center" prop="productUnit" /> -->
      <el-table-column label="分类名称" align="center" prop="productCategoryName" width="100%" />
      <el-table-column label="产品规格" align="center" prop="productStandards" width="100%" />
      <el-table-column label="价格" align="center" prop="productPrice" width="100%" />
      <!-- <el-table-column label="采购价格" align="center" prop="productPurchasePrice" width="100%" /> -->
      <el-table-column label="产品批次" align="center" prop="productBatch" width="100%" />
      <el-table-column label="入库批次" align="center" prop="inboundBatch" width="100%" />
      <el-table-column
      label="有效期"
      align="center"
      prop="productValidityDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="remark" width="100%" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <!-- <el-table-column label="流程实例的编号" align="center" prop="processInstanceId" /> -->
      <el-table-column label="创建者" align="center" prop="creatorName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:sec-account:update']"
          >
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:sec-account:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SecAccountForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SecAccountApi, SecAccountVO } from '@/api/erp/secaccount'
import SecAccountForm from './SecAccountForm.vue'
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'

/** ERP 二级库台账 列表 */
defineOptions({ name: 'SecAccount' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SecAccountVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  affairs: undefined,
  affairsId: undefined,
  deptId: undefined,
  deptName: undefined,
  fromDeptId: undefined,
  toDeptId: undefined,
  fromDeptName: undefined,
  toDeptName: undefined,
  productId: undefined,
  productUnit: undefined,
  productStandards: undefined,
  productPrice: undefined,
  productPurchasePrice: undefined,
  productBatch: undefined,
  productValidityDate: [],
  count: undefined,
  remark: undefined,
  status: undefined,
  processInstanceId: undefined,
  creator: undefined,
  createTime: [],
  creatorName: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const deptList = ref<Tree[]>([]) // 树形结构
  const parentId = ref(100)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SecAccountApi.getSecAccountPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SecAccountApi.deleteSecAccount(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SecAccountApi.exportSecAccount(queryParams)
    download.excel(data, 'ERP 二级库台账.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async() => {
  getList()
  deptList.value = handleTree(await DeptApi.getSimpleDeptLists(parentId.value))
})
</script>